package com.example.campuslink.ui.activity

import android.graphics.Bitmap
import android.os.Environment
import android.widget.SeekBar
import android.widget.Toast
import com.bumptech.glide.Glide
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.tools.Sp
import com.example.campuslink.commen.tools.SpKey
import com.example.campuslink.commen.view.DrawingBoardView
import com.example.campuslink.databinding.ActivityTuYaBinding
import com.example.campuslink.viewModel.TuYaViewModel
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.Date

class TuYaActivity : BaseActivity<ActivityTuYaBinding, TuYaViewModel>(
    TuYaViewModel::class.java,
    R.layout.activity_tu_ya,
    BR.viewModel
) {
    // TODO: 传递到发布的逻辑
    private lateinit var mDrawView: DrawingBoardView
    private var filePath: File = File("")
    override fun initView() {
        mViewModel?.context = this
        mViewModel?.initDB(this)
        mDrawView = mViewDataBinding?.coreDraw ?: DrawingBoardView(this)
        onClick()
        mViewModel?.SeekValue?.observe(this) {
            mViewDataBinding?.brushThicknessSeekBar?.setProgress(it)
            mDrawView.setPaintThickness(it)
        }
        mViewModel?.isClear?.observe(this) {
            if (it) {
                mDrawView.enableEraser(it, mViewDataBinding?.brushThicknessSeekBar?.progress ?: 20)
                Glide.with(this@TuYaActivity).load(R.drawable.eraser)
                    .into(mViewDataBinding?.ivStatus!!)
            } else {
                mDrawView.enableEraser(it, mViewDataBinding?.brushThicknessSeekBar?.progress ?: 20)
                Glide.with(this@TuYaActivity).load(R.drawable.ic_pen)
                    .into(mViewDataBinding?.ivStatus!!)
            }
        }
        mViewModel?.penColor?.observe(this) {
            when (it) {
                1 -> mDrawView.setPaintColor(resources.getColor(R.color.holo_red_dark))
                2 -> mDrawView.setPaintColor(resources.getColor(R.color.holo_blue_dark))
                3 -> mDrawView.setPaintColor(resources.getColor(R.color.holo_orange_light))
                4 -> mDrawView.setPaintColor(resources.getColor(R.color.holo_green_dark))
                5 -> mDrawView.setPaintColor(resources.getColor(R.color.black))
            }
        }
    }

    fun onClick() {
        mViewDataBinding?.brushThicknessSeekBar?.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                mViewModel?.setSeekValue(p1)
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {

            }

            override fun onStopTrackingTouch(p0: SeekBar?) {

            }

        })
        mViewDataBinding?.ivStatus?.setOnClickListener({
            mViewModel?.setClear()
        })
        mViewDataBinding?.ivClose?.setOnClickListener({
            finish()
        })
        mViewDataBinding?.ivRedPen?.setOnClickListener({
            mViewModel?.setPenColor(1)
        })
        mViewDataBinding?.ivBluePen?.setOnClickListener({
            mViewModel?.setPenColor(2)
        })
        mViewDataBinding?.ivYellowPen?.setOnClickListener({
            mViewModel?.setPenColor(3)
        })
        mViewDataBinding?.ivGreenPen?.setOnClickListener({
            mViewModel?.setPenColor(4)
        })
        mViewDataBinding?.ivBlackPen?.setOnClickListener({
            mViewModel?.setPenColor(5)
        })
        mViewDataBinding?.btnClean?.setOnClickListener({
            mDrawView.clearCanvas(mViewDataBinding?.brushThicknessSeekBar?.progress ?: 20)
            mViewModel?.setSeekValue(mViewDataBinding?.brushThicknessSeekBar?.progress ?: 20)
        })
        mViewDataBinding?.ivReBack?.setOnClickListener({
            mDrawView.undoLastStep()
            mViewModel?.setSeekValue(mViewDataBinding?.brushThicknessSeekBar?.progress ?: 20)
        })
        mViewDataBinding?.ivUnBack?.setOnClickListener({
            mDrawView.redoLastStep()
            mViewModel?.setSeekValue(mViewDataBinding?.brushThicknessSeekBar?.progress ?: 20)
        })
        mViewDataBinding?.btnKeepPhoto?.setOnClickListener({
            saveFile()
        })
        mViewDataBinding?.btnNext?.setOnClickListener({
//            val allPath = Sp.getStringArray(this, SpKey.IMAGE_PATH.key, arrayOf())?.toMutableList()
//            allPath?.add(filePath.absolutePath) // 添加新路径
//            Sp.saveStringArray(
//                this,
//                SpKey.IMAGE_PATH.key,
//                allPath?.toTypedArray() ?: arrayOf(filePath.absolutePath)
//            )
//            Sp.saveStringArray(this,SpKey.IMAGE_PATH.key, arrayOf(filePath.absolutePath))
            mViewModel?.jumpToInvitationPublicActivity(filePath.absolutePath)
        })
    }

    fun saveFile() {
        val drawingBitmap: Bitmap = mDrawView.drawingBitmap ?: return
        try {
            filePath = File(
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                    .toString() + "/" + SimpleDateFormat("yyyyMMddHHmmss").format(
                    Date()
                ) + "tuYaDraw" + ".png"
            )
            val outputStream: FileOutputStream = FileOutputStream(filePath)
            drawingBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
            outputStream.flush()
            outputStream.close()
            Toast.makeText(this@TuYaActivity, "保存成功!", Toast.LENGTH_SHORT).show()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
}